<?php
namespace Api\Controller;

use Think\Controller;
use Think\Exception;

class IndexController extends Controller
{

    protected $cookbook_model;
    protected $banner_model;
    protected $classify_model;
    protected $praise_model;
    protected $review_model;

    function __construct()
    {
        parent::__construct();
        $this->cookbook_model = D("Common/Cookbook");
        $this->banner_model = D("Common/Banner");
        $this->classify_model = D("Common/Classify");
        $this->praise_model = D("Common/Praise");
        $this->review_model = D("Common/Review");
    }

    //获取首页数据
    public function index()
    {
        //获取banner
        $sql1 = "SELECT cb.* ,cc.`name` cookname FROM cmf_banner cb
        LEFT JOIN cmf_cookbook cc ON cc.id = cb.cook_id
        WHERE cb.classify_id = 0  ORDER BY listorder DESC";
        $banner = $this->banner_model->query($sql1);
        //获取一级分类
//        $classify = $this->classify_model->where("pid = 0")->order('listorder asc')->select();

        //获取精品菜谱列表
        $sql = "SELECT cc.* ,cd.name AS name_type FROM cmf_cookbook cc LEFT JOIN cmf_classify cd ON cc.classify_pid = cd.id where cc.id=cc.id ORDER BY listorder DESC";
        $cookbooksss = $this->cookbook_model->query($sql);
        //查询每个菜谱收藏数
        foreach ($cookbooksss as $vo) {
            $id = $vo['id'];
            $praiseCount = $this->praise_model->where("cook_id = $id")->count();
            $vo['praiseCount'] = $praiseCount;
            $cookbookss[] = $vo;
        }
        //查询每个菜谱留言数
        foreach ($cookbookss as $vo) {
            $id = $vo['id'];
            $reviewCount = $this->review_model->where("cook_id = $id and review_id = 0")->count();
            $vo['reviewCount'] = $reviewCount;
            $cookbooks[] = $vo;
        }

        //获取最新菜谱列表
//        $newcookbooksss = $this->cookbook_model->order('create_time DESC')->limit(10)->select();
        //查询每个菜谱点赞数
//        foreach ($newcookbooksss as $vo) {
//            $id = $vo['id'];
//            $praiseCount = $this->praise_model->where("cook_id = $id")->count();
//            $vo['praiseCount'] = $praiseCount;
//            $newcookbookss[] = $vo;
//        }
        //查询每个菜谱留言数
//        foreach ($newcookbookss as $vo) {
//            $id = $vo['id'];
//            $reviewCount = $this->review_model->where("cook_id = $id  and review_id = 0")->count();
//            $vo['reviewCount'] = $reviewCount;
//            $newcookbooks[] = $vo;
//        }
        $data['banner'] = $banner;
//        $data['classify'] = $classify;
        $data['jingcookbooks'] = $cookbooks;
//        $data['newcookbooks'] = $newcookbooks;

        $this->returnok($data, 'ok');
    }
    //获取首页数据
    public function getNewcookbooks()
    {
        $page = I('page');
        $pages = ($page - 1) * 8;
        //获取最新菜谱列表
        $newcookbooksss = $this->cookbook_model->order('create_time DESC')->limit($pages,8)->select();
        //查询每个菜谱点赞数
        foreach ($newcookbooksss as $vo) {
            $id = $vo['id'];
            $praiseCount = $this->praise_model->where("cook_id = $id")->count();
            $vo['praiseCount'] = $praiseCount;
            $newcookbookss[] = $vo;
        }
        //查询每个菜谱留言数
        foreach ($newcookbookss as $vo) {
            $id = $vo['id'];
            $reviewCount = $this->review_model->where("cook_id = $id  and review_id = 0")->count();
            $vo['reviewCount'] = $reviewCount;
            $newcookbooks[] = $vo;
        }
//        $data['newcookbooks'] = $newcookbooks;

        $this->returnok($newcookbooks, 'ok');
    }

}
